package com.iwhalecloud.bss.kite.cucc.service.attr.handler;

import com.iwhalecloud.bss.kite.common.constant.SymbolConsts;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.client.api.attr.IUnicomRegionService;
import com.iwhalecloud.bss.kite.cucc.client.dto.attr.UnicomRegion;
import com.iwhalecloud.bss.kite.cucc.client.dto.attr.UnicomRegionQry;
import com.iwhalecloud.bss.kite.cucc.service.context.CuccServiceContext;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttrValue;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.attr.handler.AbstractAttrHandler;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @Classname InitiatorRegionAttrHandler
 * @Description 五大双线落地方产品，发起方地市属性，省-市-县 格式，取原单用户归属地值，添加获取枚举值名称方法 getValueDesc
 * @Author zhang.song
 * @Date 2021-07-16 10:09
 */
public class InitiatorRegionAttrHandler extends AbstractAttrHandler<KiteInst, AttrData> {

    @Resource
    private IUnicomRegionService unicomRegionService;

    /**
     * 落地方产品，发起方地市，显示省-市，接口传地市编码，初始化赋值时，若仅有地市编码，查询上级省份编码
     */
    @Override
    public void before(KiteAttr attr, KiteInst inst, String value, AttrData data) {
        if(KiteStringUtils.contains(value, SymbolConsts.COMMA)) {
            value = value.split(SymbolConsts.COMMA)[1];
        }
        if(KiteStringUtils.isNotEmpty(value)) {
            String regionCode = value;

            UnicomRegionQry unicomRegionQry = new UnicomRegionQry();
            unicomRegionQry.setLocationType("20");
            List<UnicomRegion> list = unicomRegionService.innerSubRegionFromPlatform(unicomRegionQry);
            if(list != null && list.size() > 0) {
                String tempValue = value;
                String provinceCode = Optional.ofNullable(list.stream().filter(unicomRegion ->
                    KiteStringUtils.isEqual(tempValue, unicomRegion.getRegionCode())).findAny().orElse(null))
                    .map(UnicomRegion::getParRegionCode).orElse(null);

                if(KiteStringUtils.isNotEmpty(provinceCode)) {
                    regionCode = provinceCode + SymbolConsts.COMMA + regionCode;
                }
            }
            String valueDesc = this.getValueDesc(attr, inst, regionCode, data);

            List<KiteAttrValue> kiteAttrValues = new ArrayList<>();
            KiteAttrValue kiteAttrValue = new KiteAttrValue();
            kiteAttrValue.setAttrValue(value);
            kiteAttrValue.setAttrValueName(KiteStringUtils.isEmpty(valueDesc) ? value : valueDesc);
            kiteAttrValues.add(kiteAttrValue);

            attr.setAttrValues(kiteAttrValues);
            attr.setValueDesc(valueDesc);
        }

        super.before(attr, inst, value, data);
    }

    /**
     * 获取页面展示名称
     *
     * @param attr 实例属性入参
     * @param inst 当前操作的实例对象
     * @param value 设置的值
     * @param data 属性处理入参，可以根据需要传入需要的业务数据
     * @return 属性页面展示名称
     */
    @Override
    public String getValueDesc(KiteAttr attr, KiteInst inst, String value, AttrData data) {
        if (KiteStringUtils.isEmpty(value)) {
            return value;
        }
        if(KiteStringUtils.equalsAny(value,",",",,")){
            return "";
        }

        String regionCode = value;
        if(!KiteStringUtils.contains(regionCode, SymbolConsts.COMMA)) {
            UnicomRegionQry unicomRegionQry = new UnicomRegionQry();
            unicomRegionQry.setLocationType("20");
            List<UnicomRegion> list = unicomRegionService.innerSubRegionFromPlatform(unicomRegionQry);
            if(list != null && list.size() > 0) {
                String provinceCode = Optional.ofNullable(list.stream().filter(unicomRegion ->
                    KiteStringUtils.isEqual(value, unicomRegion.getRegionCode())).findAny().orElse(null))
                    .map(UnicomRegion::getParRegionCode).orElse(null);

                if(KiteStringUtils.isNotEmpty(provinceCode)) {
                    regionCode = provinceCode + SymbolConsts.COMMA + value;
                }
            }
        }

        List<List<UnicomRegion>> lists = new ArrayList<>();
        List<UnicomRegion> unicomRegions = CuccServiceContext.getRegions(inst.getSpecId());
        if (KiteListUtils.isEmpty(unicomRegions)) {
            unicomRegions = unicomRegionService.subRegionFromPlatform(new UnicomRegionQry(inst.getSpecId()));
            CuccServiceContext.setRegions(inst.getSpecId(), unicomRegions);
        }
        lists.add(unicomRegions);

        String key = KiteStringUtils.concatWithNnderline("RegionCode", regionCode);
        List<List<UnicomRegion>> innerUnicomRegions = CuccServiceContext.getInnerRegions(key);
        if (KiteListUtils.isEmpty(innerUnicomRegions)) {
            innerUnicomRegions = unicomRegionService.subRegionMultiFromPlatformByRegionCode(regionCode);
            CuccServiceContext.setInnerRegions(key, innerUnicomRegions);
        }
        lists.addAll(innerUnicomRegions);

        String[] regionCodes = KiteStringUtils.split(regionCode, SymbolConsts.COMMA);
        List<String> regionNames = new ArrayList<>();
        for (int i = 0; i < regionCodes.length && i < 2; i++) {
            String currentRegionCode = regionCodes[i];
            String regionName = lists.get(i).stream()
                .filter(unicomRegion -> currentRegionCode.equals(unicomRegion.getRegionCode())).findAny()
                .map(UnicomRegion::getRegionName).orElse("");
            if(KiteStringUtils.isNotEmpty(regionName)) {
                regionNames.add(regionName);
            }
        }

        String valueDesc = KiteStringUtils.join(regionNames, SymbolConsts.HYPHEN);
        if(KiteStringUtils.isNotEmpty(valueDesc)) {
            List<KiteAttrValue> kiteAttrValues = attr.getAttrValues();
            if(!kiteAttrValues.stream().anyMatch(kiteAttrValue -> KiteStringUtils.isEqual(value, kiteAttrValue.getAttrValue()))) {
                KiteAttrValue kiteAttrValue = new KiteAttrValue();
                kiteAttrValue.setAttrValue(value);
                kiteAttrValue.setAttrValueName(valueDesc);
                kiteAttrValues.add(kiteAttrValue);

                attr.setAttrValues(kiteAttrValues);
            }
            attr.setValueDesc(valueDesc);
        }
        return valueDesc;
    }
}
